layui.use(['form','layer','upload','laydate','treeSelect','layedit','tableSelect','table'],function(){
        var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var treeSelect= layui.treeSelect;//树形下拉框



    treeSelect.render({
        elem: '#categoryName',
        data: '/admin/productCategory/treelist',
        type: 'get',
        placeholder: '默认添加首级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $.get("/admin/product/getProductListByCateNotLay?page=1&limit=10000&categoryId="+data.current.id,function(d) {
                $("#produceName").html("");
                var optionstring = "";
                $.each(d, function(i,item){
                    optionstring += "<option value=\"" + item.produceId + "\" >" + item.produceName + "</option>";
                });
                $("#produceName").html(optionstring);
                var obj=$("#produceName option")
                for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                    if(obj[i].selected==true) {
                        $("#produceId").val(obj[i].value)
                        $.get("/admin/product/getProduct?productId="+obj[i].value,function(d) {
                            $("#unit").val(d.unit)
                        });
                    }
                }
                form.on('select(test)', function(data){
                    $.get("/admin/product/getProduct?productId="+data.value,function(d) {
                        $("#produceId").val(data.value)
                        $("#unit").val(d.unit)
                    });
                });

                form.render('select');
            });
        },
        success: function (data) { // 加载完成后的回调函数
            $.get("/admin/product/getProductListByCateNotLay?page=1&limit=10000&categoryId="+data.data[0].id,function(d) {
                $("#produceName").html("");
                var optionstring = "";
                $.each(d, function(i,item){
                    optionstring += "<option value=\"" + item.produceId + "\" >" + item.produceName + "</option>";
                });

                $("#produceName").html(optionstring);

                var obj=$("#produceName option")
                for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
                    if(obj[i].selected==true) {
                        $("#produceId").val(obj[i].value)
                        $.get("/admin/product/getProduct?productId="+obj[i].value,function(d) {
                            $("#unit").val(d.unit)
                        });
                    }
                }
                form.on('select(test)', function(data){
                    $.get("/admin/product/getProduct?productId="+data.value,function(d) {
                        $("#produceId").val(data.value)
                        $("#unit").val(d.unit)
                    });

                });
                form.render('select');
            });

        }
    });

    var date = new Date();
    laydate.render({
        elem: '#useDate'
        ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()   //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
    });



form.on("submit(addProduce)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
     $.post("/admin/productUsed/add",{
         produceId: $("#produceId").val(),
         userId: $("#userId").val(),
         useNum: $("#useNum").val(),
         useDate : new Date(Date.parse($("#useDate").val())),
         remarks:$("#remarks").val()
         },function(res){
            //成功后返回的数据res，这里可以写一些成功后的操作
         })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("产品添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })

})